package com.easy.controller;

import com.easy.bean.Cart;
import com.easy.bean.User;
import com.easy.service.ICartService;
import com.easy.util.LayData;
import com.easy.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("cart")
public class CartController {
    @Autowired
    ICartService cartService;
    @RequestMapping("add")
    @ResponseBody
    public LayData add(HttpSession session ,Cart cart){
        User loginUser=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        cart.setUserid(loginUser.getId());
        return LayData.success(cartService.add(cart));
    }
    @RequestMapping("edit")
    @ResponseBody
    public LayData edit(HttpSession session ,Cart cart){
        User loginUser=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        cart.setUserid(loginUser.getId());
        Cart editCart=cartService.edit(cart);
        return LayData.success(200,"数量设置成功",editCart.getNumber());
    }
    @RequestMapping("delete")
    @ResponseBody
    public LayData delete(HttpSession session ,Cart cart){
        User loginUser=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        cart.setUserid(loginUser.getId());
        return LayData.success(cartService.delete(cart));
    }
    @RequestMapping("getCart")
    @ResponseBody
    public LayData getCart(HttpSession session ,Cart cart){
        return LayData.success(cartService.getCart(cart));
    }
    @RequestMapping("getCarts")
    @ResponseBody
    public LayData getCarts(HttpSession session ){
        User loginUser=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        int userid=loginUser.getId();
        List<Cart> list=cartService.getCarts(userid);
       return LayData.success(list);
    }
}
